<?php
namespace app\api\model;

use think\Model;
use think\Db;
class UserModel extends Model
{
    function  login($username, $password) {
        $result = Db::name('user')->where('username', $username)->where('password', md5($password))->find();
        return $result;
    }
    function  register($username,$password,$address,$phone,$gender){
          if ($gender=='1'){
              $gender1=1;
          }
          else
          {
              $gender1=0;
          }
          $data=[
              'username'=> $username,
              'password'=>md5($password),
              'picture'=> null,
              'email' =>null,
              'address'=>$address,
              'phone' =>$phone,
              'gender'=>$gender1,
              'createtime'=>date("YmdHis"),
              'sport'=>0,
              'state'=>1

          ];
        $num=Db::name("user")->where("username='$username'")->count();
        if ($num) {
           return -1;
        }
        else
        {
            $res=Db::name("user")->insert($data);
            if($res){
                return 1;

            }else{
                return -1;
            }
        }

    }

    function gameenroll($userid, $gameid) {
        $data=[
            'userid'=>$userid,
            'gameid'=>$gameid,
        ];
        $result=Db::name('game_enroll')->insert($data);
        if($result){
            return  ['success'=>1];

        }else{
            return  ['success'=>0];
        }
    }

    function getuser($userid)
    {
        $result = Db::name('user')->where('userid', $userid)->find();
        return $result;
    }

    /*function updateNickname($userid, $nickname){
        $data = ['nickname'=>$nickname];
        $result = Db::name('user')->where('userid',$userid)->update($data);
        return ['success'=>$result];
    }*/

    function updateUser($userid,$nickname,$password,$address,$phone){

        $data=['nickname'=>$nickname,'password'=>md5($password), 'address'=>$address, 'phone'=>$phone];
        $result= Db::name('user')->where('userid',$userid)->update($data);
        return ['success'=>$result];
    }
}